package cn.ctbu.xiaopeng.studentscoremanage.service;

import cn.ctbu.xiaopeng.studentscoremanage.anno.Log;
import cn.ctbu.xiaopeng.studentscoremanage.dao.UserRepository;
import cn.ctbu.xiaopeng.studentscoremanage.enums.ResultEnum;
import cn.ctbu.xiaopeng.studentscoremanage.exception.ResultException;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.dto.UserQueryDTO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.entity.Student;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.entity.User;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.UserPageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author xiaopeng
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class UserService {

    private final UserRepository userRepository;


    /**
     * 登录验证-验证用户名和密码是否匹配
     * @param userName
     * @param password
     * @return
     */
    public User validateUserNameAndPsw(String userName, String password) throws Exception{
        User user = userRepository.findLockedUserByUserName(userName);
        if (user != null && user.getPassword().equals(password)) {
            //匹配
            return user;
        }else{
            throw new ResultException(ResultEnum.LOGIN_ERROR);
        }
    }

    /**
     * 修改用户状态
     * @param userId
     * @param isLocked
     * @return
     */
    public void changeUserIsLocked(String userId, Integer isLocked) {
        userRepository.changeUserIsLocked(userId, isLocked);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Log
    public User insert(User user) {
        //获取当前时间
        user.setRegisterTime(LocalDateTime.now());
        return userRepository.save(user);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @Log
    public User update(User user) {
        //获取当前时间
        user.setRegisterTime(LocalDateTime.now());
        return userRepository.save(user);
    }

    /**
     * 根据id删除用户
     *
     * @param id
     */
    @Log
    public void deleteById(Integer id) {
        userRepository.deleteById(id);
    }

    /**
     * 根据id批量删除用户
     *
     * @param ids
     */
    @Log
    @Transactional
    public void deleteUsersByIds(List<Integer> ids) {
        if (ids != null && !ids.isEmpty()) {
            userRepository.deleteByIds(ids);
        } else {
            throw new ResultException("用户ID列表不能为空");
        }
    }

    /**
     * 分页查询
     *
     * @param pageable
     * @return
     */
    @Log
    public Page<User> getAllWithPage(Pageable pageable) {
        return userRepository.findAll(pageable);
    }

    /**
     * 分页查询
     *
     * @param example
     * @param pageable
     * @return
     */
    @Log
    public Page<User> getAllWithPage(Example<User> example, Pageable pageable) {
        return userRepository.findAll(example, pageable);
    }

    /**
     * 条件查询
     *
     * @param userQueryDTO
     * @param pageable
     * @return
     */
    public Page<UserPageVO> getUserPageByConditions(UserQueryDTO userQueryDTO, Pageable pageable) {
        return userRepository.getUserPageByConditions(userQueryDTO, pageable);
    }
}
